using System;
using System.Collections;
using System.Reflection;
using System.Web;

namespace Jaws.Core.Web
{
    public class Global : HttpApplication
    {
        /// <summary>
        /// Hooks up the available extensions located in the App_Code folder.
        /// An extension must be decorated with the ExtensionAttribute to work.
        /// <example>
        ///  <code>
        /// [Extension("Description of the SomeExtension class")]
        /// public class SomeExtension
        /// {
        ///   //There must be a parameterless default constructor.
        ///   public SomeExtension()
        ///   {
        ///     //Hook up to the BlogEngine.NET events.
        ///   }
        /// }
        /// </code>
        /// </example>
        /// </summary>
        protected void Application_Start(object sender, EventArgs e)
        {
            ArrayList codeAssemblies = Utils.CodeAssemblies();
            foreach (Assembly a in codeAssemblies)
            {
                Type[] types = a.GetTypes();
                foreach (Type type in types)
                {
                    object[] attributes = type.GetCustomAttributes(typeof (ExtensionAttribute), false);
                    for (int i = 0; i < attributes.Length; i++)
                    {
                        a.CreateInstance(type.FullName);
                    }
                }
            }
        }
    }
}